package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.echoesnet.echoesshortvideo.Api.ApiException;
import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.Beans.ErroInfoBean;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Repositories.TinyVideoInfoRepository;
import com.echoesnet.echoesshortvideo.Repositories.VideoPlayRepository;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 * 负责驱动UI
 *
 * @author lc
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 18:05
 * @description
 */
public class VideoPlayViewModel extends ViewModel
{
    private MutableLiveData<String> likeResult = new MutableLiveData();
    private MutableLiveData<String> commentResult = new MutableLiveData();
    private MutableLiveData<String> focusResult = new MutableLiveData();
    private MutableLiveData<String> deleteVideo = new MutableLiveData();
    private MutableLiveData<String> shareVideo = new MutableLiveData();
    private MutableLiveData<ErroInfoBean> errInfo = new MutableLiveData<>();

    private VideoPlayRepository uDetailRepository;

    public LiveData<String> getLikeData()
    {
        return likeResult;
    }

    public LiveData<ErroInfoBean> getErrInfo()
    {
        return errInfo;
    }

    public LiveData<String> getCommentDetail()
    {
        return commentResult;
    }

    public LiveData<String> getFocusResult()
    {
        return focusResult;
    }

    public MutableLiveData<String> getDeleteVideo()
    {
        return deleteVideo;
    }

    public MutableLiveData<String> getShareVideo()
    {
        return shareVideo;
    }

    private TinyVideoInfoRepository tinyVideoInfoRepository;

    public VideoPlayViewModel()
    {
        this.uDetailRepository = new VideoPlayRepository();
        this.tinyVideoInfoRepository = new TinyVideoInfoRepository();
    }

    /**
     * 点赞视频
     *
     * @param videoId 视频ID
     */
    public void startLikeVideo(String videoId)
    {
        uDetailRepository.likeVideo(Api.VIDEOC_LIKEVIDEO, videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                likeResult.setValue("0");
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                super.onHandledError(apiE);
                ErroInfoBean err = new ErroInfoBean(Api.VIDEOC_LIKEVIDEO, apiE.getErrorCode(), apiE.getErrBody());
                errInfo.setValue(err);
            }
        });
    }

    /**
     * 评论视频
     *
     * @param videoId        视频ID
     * @param commentContent 评论内容
     */
    public void startCommentVideo(String videoId, String commentContent)
    {
        uDetailRepository.commentVideo(Api.VIDEOC_COMMENT_VIDEO, videoId, commentContent, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                commentResult.setValue("0");
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                super.onHandledError(apiE);
                ErroInfoBean err = new ErroInfoBean(Api.VIDEOC_COMMENT_VIDEO, apiE.getErrorCode(), apiE.getErrBody());
                errInfo.setValue(err);
            }
        });
    }

    /**
     * 关注用户
     *
     * @param toUserId 用户ID
     */
    public void focusPerson(String toUserId)
    {
        uDetailRepository.focus(Api.USERC_CHECK_FOLLOW, toUserId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                focusResult.setValue("0");
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                super.onHandledError(apiE);
                ErroInfoBean err = new ErroInfoBean(Api.USERC_CHECK_FOLLOW, apiE.getErrorCode(), apiE.getErrBody());
                errInfo.setValue(err);
            }
        });
    }

    /**
     * 删除视频
     *
     * @param videoId 用户ID
     */
    public void deleteVideo(String videoId)
    {
        uDetailRepository.deleteVideo(Api.VIDEOC_DELETE_VIDEO, videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                deleteVideo.setValue("0");
                tinyVideoInfoRepository.deleteVideoInfo(videoId);
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                super.onHandledError(apiE);
                ErroInfoBean err = new ErroInfoBean(Api.VIDEOC_DELETE_VIDEO, apiE.getErrorCode(), apiE.getErrBody());
                errInfo.setValue(err);
            }
        });
    }
    /**
     * 分享视频
     *
     * @param videoId 用户ID
     */
    public void shareVideo(String videoId)
    {
        uDetailRepository.shareVideo(Api.VIDEOC_SHARE_VIDEO, videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                shareVideo.setValue("0");
            }
        });
    }

}
